package com.jijia.webbase.exception;

import com.jijia.webbase.config.response.JsonResult;
import com.jijia.webbase.exception.custException.HaoWanException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.AuthenticationException;
import javax.validation.UnexpectedTypeException;
import java.nio.file.AccessDeniedException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 8. 全局异常处理
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {


    @ExceptionHandler(HaoWanException.class)
    public JsonResult handleHaoWanException(HaoWanException e) {
        return JsonResult.ok(e.getMsg());
    }

    @ExceptionHandler(AuthenticationException.class)
    public JsonResult handleAuthenticationException(AuthenticationException e) {
        return JsonResult.ok(e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public JsonResult handleAccessDeniedException(AccessDeniedException e) {
        return JsonResult.ok(e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public JsonResult handleException(BindException e) {
        e.printStackTrace();
        return JsonResult.ok(e.getMessage());
    }

    //UnexpectedTypeException
    @ExceptionHandler(UnexpectedTypeException.class)
    public JsonResult handleException(UnexpectedTypeException e) {
        e.printStackTrace();
        return JsonResult.ok(e.getMessage());
    }
    //ConstraintViolationException
    @ExceptionHandler(javax.validation.ConstraintViolationException.class)
    public JsonResult handleException(javax.validation.ConstraintViolationException e) {
        e.printStackTrace();
        String msg = e.getMessage();
        return JsonResult.ok(msg);
    }


    @ExceptionHandler(RuntimeException.class)
    public JsonResult handleRuntimeException(RuntimeException e) {
        e.printStackTrace();
        return JsonResult.ok(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public JsonResult handleRuntimeException(Exception e) {
        e.printStackTrace();
        return JsonResult.ok(e.getMessage());
    }
}